package com.lejin.usercenter.service.impl;


import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lejin.usercenter.entity.SysUser;
import com.lejin.usercenter.mapper.SysUserMapper;
import com.lejin.usercenter.service.ISysUserService;
import com.lejin.vo.CheckPwdVo;
import com.lejin.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luminghua
 * @since 2022-04-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ResultVO findSysUserByUid(String userId) {

        if (StringUtils.isEmpty(userId)){
            return new ResultVO(false,"用户Id不能为空");
        }


        SysUser sysUser = new LambdaQueryChainWrapper<SysUser>(sysUserMapper).eq(SysUser::getUserId, userId).one();
        return new ResultVO(true,"success",sysUser);
    }

    @Override
    public ResultVO updateSysUser(SysUser sysUser) {
        if (StringUtils.isEmpty("sysUser")){


            return new ResultVO(false,"用户信息不能为空");
        }



        SysUser sysUser1 = new LambdaQueryChainWrapper<SysUser>(sysUserMapper).eq(SysUser::getUserId, sysUser.getUserId()).one();
        if (StringUtils.isEmpty(JSONUtil.toJsonStr(sysUser1))){
            int insert = sysUserMapper.insert(sysUser);
            if (insert>0){

                return new ResultVO(true,"添加成功!");

            }
        }
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", sysUser.getUserId());

        int update = sysUserMapper.update(sysUser, updateWrapper);
        if (update>0){
            return new ResultVO(true,"修改成功");
        }

        return new ResultVO(false,"修改失败！");

    }

    @Override
    public ResultVO checkPwd(CheckPwdVo checkPwdVo) {
        if (StringUtils.isEmpty(checkPwdVo.getNewPwd())){



            return new ResultVO(false,"密码不能为空");
        }else if (checkPwdVo.getPassword().equals(checkPwdVo.getNewPwd())){


            return new ResultVO(false,"密码重复请重试");
        }else if (StringUtils.isEmpty(checkPwdVo.getAginPwd())){
                 return new ResultVO(false,"请确认密码");
        }else if (!checkPwdVo.getNewPwd().equals(checkPwdVo.getAginPwd())){

                return new ResultVO(false,"密码不一致");
        }else {
            return new ResultVO(true,"可以修改密码");
        }








    }

    @Override
    public ResultVO changePwd(CheckPwdVo checkPwdVo) {

        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", checkPwdVo.getUserId());
        SysUser sysUser = new SysUser();
        sysUser.setPassword(checkPwdVo.getAginPwd());
        int update = sysUserMapper.update(sysUser, updateWrapper);
        if (update>0){
            return new ResultVO(true,"success");
        }
        return new ResultVO(false,"修改失败");
    }

    @Override
    public ResultVO avatUpload(MultipartFile avtImg, HttpServletRequest request) {

        //服务器的绝对路劲
//        String realPath = request.getServletContext().getRealPath("/images/");


//        D:\nginx-1.18.0\lejinusercenter\images

        String realPath="D:\\nginx-1.18.0\\lejinusercenter\\images";
        //上传文件名
        String originalFilename = avtImg.getOriginalFilename();

        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String replace = UUID.randomUUID().toString().replace("-", "");

        String newName=replace+substring;

        System.out.println(newName);

        System.out.println(realPath);


        File newFile = new File(realPath, newName);

        if (!newFile.getParentFile().exists()){
            newFile.getParentFile().mkdirs();
        }
        try {
            avtImg.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("sub"+realPath+"newName"+newName);



        return new ResultVO(true,"success","images\\"+newName);
    }
}
